﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Common;
using System.Web.SessionState;
using System.Dynamic;
using Model;
using DAL;
using System.Data;
using System.Dynamic;
using System.Net;
using System.Text;
using System.Web.SessionState;
using JPushAPI;
using WxPayAPI;

namespace UI.tools
{
    /// <summary>
    /// API 的摘要说明
    /// </summary>
    public class API : IHttpHandler, IRequiresSessionState
    {
        static Dictionary<string, Func<int,string>> hs = new Dictionary<string, Func<int,string>>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = JobRequest.GetRequestString("act");
            string token = JobRequest.GetRequestString("token");
            string result = string.Empty;

            if (Commons.CheckSign())
            {
                int userid = 0;
                //登录、注册等不检查token
                if (Commons.NoCheckToken.Contains(action))
                {
                    result = hs[action](userid);
                }
                else
                {
                    userid = UserDAL.CheckToken(token);
                    if (userid == 0)
                    {
                        result = Commons.GetFaild("身份验证失败");
                    }
                    else
                    {
                        result = hs[action](userid);
                    }
                }                
            }
            else
            {
                result = Commons.GetFaild("身份验证失败");
            }

            context.Response.Write(result);
            context.Response.End();
        }
        static API()
        {
            /*登陆注册*/
            #region 登陆
            hs.Add("Login", (int userid) =>
            {
                try
                {
                    //参数
                    string username = JobRequest.GetRequestString("username"); //用户名
                    string userpwd = JobRequest.GetRequestString("userpwd"); //密码
                    int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                    //参数
                    UserInfo info = UserDAL.Login(username, DESEncrypt.Encrypt(userpwd));
                    if (info.ID > 0)
                    {
                        if (info.Enable==0)
                        {
                            return Commons.GetFaild("您的账号已经被禁用，暂时不能登陆");
                        }
                        if (source == 1 || source == 0)
                        {
                            B.MyUser = info;
                        }
                        info.Token=Commons.GetToken();
                        CommonDAL.UpdateColumnByID(info.ID,"Token",info.Token,"[user]");
                        return Commons.GetSuccess("登陆成功", info);
                    }
                    else
                    {
                        return Commons.GetFaild("用户名或密码错误");
                    }
                }
                catch (Exception ex)
                {
                    return Commons.GetFaild(ex.Message);
                }
            });
            #endregion
            #region 注册
            hs.Add("Regist", (int userid) =>
            {
                //参数
                string username = JobRequest.GetRequestString("username"); //用户名
                string userpwd = JobRequest.GetRequestString("userpwd"); //密码
                string realname = JobRequest.GetRequestString("realname"); //真实姓名
                string mobile = JobRequest.GetRequestString("mobile"); //手机号
                string headimg = JobRequest.GetRequestString("headimg"); //头像
                string sex = JobRequest.GetRequestString("sex"); //性别
                int enable = JobRequest.GetRequestInt("enable"); //是否可用
                string smscode = JobRequest.GetRequestString("smscode"); //验证码
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                string code = CommonDAL.FindColumnByColumn("smscode", "mobile", mobile, "smscode");
                if (code != smscode || smscode == "")
                {
                    return Commons.GetFaild("验证码不正确");
                }

                string uid = CommonDAL.FindColumnByColumn("ID", "Mobile", mobile, "[user]");
                if (uid != "")
                {
                    return Commons.GetFaild("该手机号码已注册");
                }

                UserInfo info = new UserInfo()
                {
                    UserName = username,
                    UserPwd = userpwd,
                    RealName = realname,
                    Mobile = mobile,
                    HeadImg = headimg,
                    Sex = sex,
                    Enable = enable,
                    Token = Commons.GetToken(),
                    CreateDate = DateTime.Now
                };
                ID = UserDAL.Save(info);
                if (ID > 0)
                {
                    info.ID = ID;
                    //SendSms(mobile, "您已成功注册焙友之家，您的账号是：" + mobile + "，密码是：" + userpwd + "，请妥善保存");
                    return Commons.GetSuccess("注册成功", info);
                }
                else
                {
                    return Commons.GetFaild("注册失败");
                }
            });
            #endregion
            #region 发送验证码
            hs.Add("Send_SmsCode", (int userid) =>
            {
                //参数
                string mobile = JobRequest.GetRequestString("mobile"); //手机号码
                //参数
                bool isexist = UserDAL.CheckMobile(mobile);
                if (isexist)
                {
                    return Commons.GetFaild("该手机号已绑定其他账号，不能重复绑定");
                }
                string smscode = new Random().Next(100000, 999999).ToString();
                //发送短信代码
                SendSms(mobile, "验证码：" + smscode + "，您正在注册，需要进行效验，如非本人操作请忽略。");
                //发送短信代码
                bool ok = true;
                ok = UserDAL.SmsCode(mobile, smscode);
                if (ok)
                {
                    return Commons.GetSuccess("发送成功", smscode);
                }
                else
                {
                    return Commons.GetFaild("发送失败");
                }
            });
            #endregion
            #region 修改密码
            hs.Add("UpdatePwd", (int userid) =>
            {
                //参数
                string oldpwd = JobRequest.GetRequestString("oldpwd"); //旧密码
                string newpwd = JobRequest.GetRequestString("newpwd"); //新密码
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                UserInfo user = new UserInfo();
                if (source == 1 || source == 0)
                {
                    user = B.MyUser;
                }
                else
                {
                    user = UserDAL.FindOneByID(userid);
                }
                
                if (user.UserPwd != oldpwd)
                {
                    return Commons.GetFaild("旧密码错误");
                }
                bool ok = CommonDAL.UpdateColumnByID(user.ID, "userpwd", DESEncrypt.Encrypt(newpwd), "[user]");
                if (ok)
                {
                    if (source == 1 || source == 0)
                    {
                        B.MyUser.UserPwd = newpwd;
                    }
                    return Commons.GetSuccess("修改成功", "");
                }
                else
                {
                    return Commons.GetFaild("修改失败");
                }
            });
            #endregion

            /*用户*/
            #region 获取用户
            hs.Add("Find_User", (int userid) =>
            {
                //参数
                int pageindex = JobRequest.GetRequestInt("pageindex"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<UserInfo> list = UserDAL.FindPage(keyword, begindate, enddate, pageindex, Commons.PageSize, "", "", out count);
                dynamic obj = new ExpandoObject();
                obj.list = list;
                obj.count = count;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 保存用户
            hs.Add("Save_User", (int userid) =>
            {

                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string username = JobRequest.GetRequestString("username"); //用户名
                string userpwd = JobRequest.GetRequestString("userpwd"); //密码
                string realname = JobRequest.GetRequestString("realname"); //真实姓名
                string sex = JobRequest.GetRequestString("sex"); //性别
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                UserInfo info = new UserInfo()
                {
                    ID = id,
                    UserPwd=DESEncrypt.Encrypt(userpwd),
                    Sex=sex,
                    CreateDate = DateTime.Now
                };
                if (id == 0)
                {
                    ID = UserDAL.Save(info);
                }
                else
                {

                    ID = UserDAL.Update(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion
            #region 修改个人信息
            hs.Add("Update_User", (int userid) =>
            {

                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                string realname = JobRequest.GetRequestString("realname"); //真实姓名
                string sex = JobRequest.GetRequestString("sex"); //性别
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.UpdateColumnByID(id, "RealName", realname, "[user]");
                ok = CommonDAL.UpdateColumnByID(id, "Sex", sex, "[user]");
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", "");
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion
            #region 获取1个用户
            hs.Add("One_User", (int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //用户ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                dynamic obj = new ExpandoObject();
                obj.id = id;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion

            /*支付*/
            #region 获取微信支付参数
            hs.Add("GetWxPayParam", (int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //订单ID
                float allmoney = JobRequest.GetRequestFloat("allmoney"); //充值金额（单位：分）
                //参数
                dynamic dy = new ExpandoObject();
                WxPayData data = new WxPayData();
                string nonce_str = WxPayApi.GenerateNonceStr();
                string timestamp = WxPayApi.GenerateTimeStamp();
                data.SetValue("body", "商户名称");
                data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
                data.SetValue("total_fee", DataConvert.ToInt(allmoney * 100));
                data.SetValue("trade_type", "APP");
                data.SetValue("attach", userid);
                data.SetValue("nonce_str", nonce_str);//随机字符串
                WxPayData result = WxPayApi.UnifiedOrder(data);
                dy.prepay_id = result.GetValue("prepay_id");
                dy.appid = WxPayConfig.APPID;
                dy.mchid = WxPayConfig.MCHID;
                dy.key = WxPayConfig.KEY;
                dy.appsecret = WxPayConfig.APPSECRET;
                WxPayData wp = new WxPayData();
                wp.SetValue("appid", WxPayConfig.APPID);//公众账号ID
                wp.SetValue("partnerid", WxPayConfig.MCHID);//商户号
                wp.SetValue("prepayid", dy.prepay_id);
                wp.SetValue("noncestr", nonce_str);//随机字符串
                wp.SetValue("timestamp", timestamp);
                wp.SetValue("package", "Sign=WXPay");
                dy.sign = wp.MakeSign();
                dy.package = "Sign=WXPay";
                dy.timestamp = timestamp;
                dy.noncestr = nonce_str;
                return Commons.GetSuccess("保存成功", dy);
            });
            #endregion
            #region 获取H5微信支付参数
            hs.Add("GetH5WxPayParam", (int userid) =>
            {
                //参数
                double allmoney = JobRequest.GetRequestDouble("allmoney"); //充值金额
                //参数
                string ip = Commons.IPAddress;
                dynamic dy = new ExpandoObject();
                WxPayData data = new WxPayData();
                string nonce_str = WxPayApi.GenerateNonceStr();
                string timestamp = WxPayApi.GenerateTimeStamp();
                data.SetValue("body", "商户名称");
                data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
                data.SetValue("total_fee", DataConvert.ToInt(allmoney * 100));
                data.SetValue("trade_type", "MWEB");
                data.SetValue("attach", userid);
                data.SetValue("nonce_str", nonce_str);//随机字符串
                data.SetValue("spbill_create_ip", ip);
                WxPayData result = WxPayApi.UnifiedOrder(data);
                dy.prepay_id = result.GetValue("prepay_id");
                dy.appid = WxPayConfig.APPID;
                dy.mchid = WxPayConfig.MCHID;
                dy.key = WxPayConfig.KEY;
                dy.appsecret = WxPayConfig.APPSECRET;
                WxPayData wp = new WxPayData();
                wp.SetValue("appid", WxPayConfig.APPID);//公众账号ID
                wp.SetValue("partnerid", WxPayConfig.MCHID);//商户号
                wp.SetValue("prepayid", dy.prepay_id);
                wp.SetValue("noncestr", nonce_str);//随机字符串
                wp.SetValue("timestamp", timestamp);
                wp.SetValue("package", timestamp);
                dy.sign = wp.MakeSign();
                dy.package = timestamp;
                dy.timestamp = timestamp;
                dy.noncestr = nonce_str;
                return Commons.GetSuccess("保存成功", dy);
            });
            #endregion
            #region 获取JS微信支付参数
            hs.Add("GetJSWxPayParam", (int userid) =>
            {
                //参数
                string openid = JobRequest.GetRequestString("openid");
                double allmoney = JobRequest.GetRequestDouble("allmoney"); //充值金额（单位：分）
                //参数
                dynamic dy = new ExpandoObject();
                try
                {
                    WxPayData data = new WxPayData();
                    string nonce_str = WxPayApi.GenerateNonceStr();
                    string timestamp = WxPayApi.GenerateTimeStamp();
                    data.SetValue("body", "商户名称");
                    data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
                    data.SetValue("total_fee", DataConvert.ToInt(allmoney * 100));
                    data.SetValue("openid", openid);
                    data.SetValue("trade_type", "JSAPI");
                    data.SetValue("attach", userid);
                    data.SetValue("nonce_str", nonce_str);//随机字符串
                    WxPayData result = WxPayApi.UnifiedOrder(data);
                    dy.prepay_id = result.GetValue("prepay_id");
                    dy.appid = WxPayConfig.APPID;
                    dy.mchid = WxPayConfig.MCHID;
                    dy.key = WxPayConfig.KEY;
                    dy.appsecret = WxPayConfig.APPSECRET;
                    WxPayData wp = new WxPayData();
                    wp.SetValue("appId", WxPayConfig.APPID);//公众账号ID
                    wp.SetValue("nonceStr", nonce_str);//随机字符串
                    wp.SetValue("timeStamp", timestamp);
                    wp.SetValue("signType", "MD5");
                    wp.SetValue("package", "prepay_id=" + dy.prepay_id);
                    dy.sign = wp.MakeSign();
                    dy.timestamp = timestamp;
                    dy.noncestr = nonce_str;
                }
                catch (Exception ex)
                {
                    Commons.Log(ex.Message);
                }
                return Commons.GetSuccess("保存成功", dy);
            });
            #endregion
            #region 获取支付宝appid和rsa
            hs.Add("GetAliPayParam", (int userid) =>
            {
                dynamic info = new ExpandoObject();
                info.appid = "2017062007526285";
                info.email = "wanggang@beiyouvip.com";
                info.notify_url = Commons.domain + "/notify.aspx";
                info.rsa = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQT4DZG7ZMec074oOYWINIi98kYqpT70yuNHSO80F+ijfCWPGRGopLQ5grzEz21IqPQfAkXUBCmkw4xZ3KJJ40aZ5FPFU4F/bRZEmpeoQ6HX2SQAqsFTzr7igzaV7H0jZhArZRMNgJ0KV9EGzYUTnmcWYMt3CDM9pEEWF6N31bMcaLL/LFgAocmv2+qD89a1lW20uixz5uQYvHhEvAXpZM8eap/rBqTp45bPGhCdBjENIDJzRhE47qZC7c7VQdsC55x/H5JBS5AZW27X6Tpx8+q3S81HGwrV5fViikSI1mWNxa1P9UNPLga795poc3G2haO+vUXy5BhZbzWDD2YH9LAgMBAAECggEAZ8iFhZ4kcMN4EgVJVprxb48IH4bFsC99GhunZ9cR1wtYJnEI6a4InzAxk+4Nu8YHEBf/uGdH+540CvA0FlhXXqJGrMxDDWBOpTBWuYfe6FylHjTod/GeKp7QUokJHPPVH9dvftx9wjFHkrXpUXi5Bz2ugp66T+FOq31EeTzG7nm6u0PIM4ZU7QeuQaGX/D+UpoOTmurW6OWtPB7LMMKJFEnfAp/aQwppQbaHrmYKu0CoLBIyb1DVaJ1tuxF7EpazrvrhE42AbzWRWWBgQYc1kUaEilh2p7P2LZdlYX2aMwk6FCVx7z5BJE+OcDLG590dPyKsFaxhCqtDrIxsxVN1IQKBgQDbuf4mI4O7fmZloHphjsKd6yvr5P5yNRYRcbXHz065DKmy+/ebKztSCab7pkss8O1oEU9GGO6xX02lLlwJy1JIQ/OnIuT3i+jPUQdztlVCJJBQwIJ+RMguB2mUOAHDtT9N8pki1v5ufI4aUQBcN8lcG8fbrX+YWvRQo6hCvhotrwKBgQCoIk57GRi+iH9mxUEr+TnQnTKnbJ+o/B2xHnK50zj11HBVgTwA1GrwheWsbmJLwHb0SxT3xJiClunVkaHrxNUkYa46SkAimpYGCjRg2Ld7axRD5ZE74KNC46MKyFYFBcqvHpAVICiWq22cHzwrraE73dDLeTEFuwxXR8zRuiyrJQKBgQDYJw08yZ0y2jKzcP5TcJRFs+7ePcSN52lROdKAwJCJ/EoIuFSpwgq2NqadUscLa+uwf67Dn41Y0g9ZGtUSEYFdc/OCjRI/79PT9rtk+o6+5adOuULmIpXmqgYT+FDMDUY8URaSrntXh5Jnkx3OmlatFOs0UWZ/I6LKnWTOW6L3IwKBgD7jPhmbBH9XzNdi1oKdILvf4G9xGLL7QhvauKy6zaZdhHrm8MuaqDk4y/RYAVo2LggoATq/hQ/rZKEmbLskD1khVCqnVR/cVFsD/B1xUgfOwHC48D7GQiqlvHku/8W3Kd2gf+H2+leI3WG06SupMRKUYCGLPgPXt8D5sSmNCmIlAoGBAK9ZVs/jQ6evjL8XdwlNZWszFzZCv4vWSnpVXRiI1Blu3GyYWlcg+dRJaK5zRchqrOfuMORqwsvVi7OmoIfCPFkJgGF1TtWKJbqxS9pokihN/t2a2xoCddCrRFS6Xo1N6R7S+z8P4I9efp6BzazMB2aNKEa/MMSvSs/gjHpvTULV";
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion
            #region 版本更新
            hs.Add("UpdateClient", (int userid) =>
            {
                dynamic dy = new ExpandoObject();
                //版本号
                dy.version = Commons.GetConfig("Version");
                //下载地址
                dy.url = Commons.domain + "/app/xinlian.apk";
                //app名字qiyebao
                dy.appname = "信联";
                string content = Commons.GetConfig("Content").Replace("\\n", "\n");
                //app更新内容
                dy.content = content == null ? "" : content;
                dy.appsize = Commons.GetConfig("AppSize");
                return SerializeHelper.ToJsonByDynamic(dy);
            });
            #endregion

        }

        //PC用
        public static void UploadAnnex(int sid, int userid, string flag, string imgs)
        {
            string[] imges = imgs.Split(',');
            if (imgs.Trim() != "")
            {
                foreach (string img in imges)
                {
                    string[] images = img.Split('*');
                    FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath(images[0]));
                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = images[1],
                        Path = images[0],
                        Size = (int)file.Length,
                        Flag = flag,
                        Type = 1,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }
            }
        }
        public static void UploadFile(int sid, int userid, string flag, string imgs)
        {
            string[] imges = imgs.Split(',');
            if (imgs.Trim() != "")
            {
                foreach (string img in imges)
                {
                    string[] images = img.Split('*');
                    FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath(images[0]));
                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = images[1],
                        Path = images[0],
                        Size = (int)file.Length,
                        Flag = flag,
                        Type = 2,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }
            }
        }
        //手机用
        public static void UploadAnnex(int sid,int userid, string flag)
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string[] keys = content.Request.Files.AllKeys;
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0 && keys[j]=="img")
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    string imgpath50 = name + "_50" + ext;
                    string imgpath100 = name + "_100" + ext;
                    string imgpath200 = name + "_200" + ext;
                    string imgpath500 = name + "_500" + ext;
                    string imgpath1000 = name + "_1000" + ext;
                    Thumbnail.MakeThumbnailImage(file, imgpath50, 50, 50);
                    Thumbnail.MakeThumbnailImage(file, imgpath100, 100, 100);
                    Thumbnail.MakeThumbnail(file, imgpath200, 200, 200);
                    Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);
                    Thumbnail.MakeThumbnailImage(file, imgpath1000, 1000, 1000);

                    AnnexInfo info = new AnnexInfo()
                    {
                        SID=sid,
                        UserID=userid,
                        Name=uploadFile.FileName,
                        Path = opath + unique + ext,
                        Size=uploadFile.ContentLength,
                        Flag=flag,
                        Type=1,
                        CreateDate=DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }

                if (uploadFile.ContentLength > 0 && keys[j] == "file")
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    if (ext == "") { continue; }
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = uploadFile.FileName,
                        Path = opath + unique + ext,
                        Size = uploadFile.ContentLength,
                        Flag = flag,
                        Type=2,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);

                }

            }
        }
        public static string UploadImg()
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string str = "";
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    string imgpath50 = name + "_50" + ext;
                    string imgpath100 = name + "_100" + ext;
                    string imgpath200 = name + "_200" + ext;
                    string imgpath500 = name + "_500" + ext;
                    string imgpath1000 = name + "_1000" + ext;
                    Thumbnail.MakeThumbnailImage(file, imgpath50, 50, 50);
                    Thumbnail.MakeThumbnailImage(file, imgpath100, 100, 100);
                    Thumbnail.MakeThumbnail(file, imgpath200, 200, 200);
                    Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);
                    Thumbnail.MakeThumbnailImage(file, imgpath1000, 1000, 1000);
                    str = opath + unique + ext;
                }

            }
            return str;
        }
        static string SendSms(string PhoneNumber, string SMSContent)
        {
            string tmp = "";
            if (PhoneNumber != "")
            {
                string accountname = "tianze";
                string accountpwd = "tianze8899";
                Encoding encoding = Encoding.GetEncoding("gb2312");
                string postData = "";
                SMSContent = System.Web.HttpUtility.UrlEncode(SMSContent, System.Text.Encoding.GetEncoding("gb2312"));
                byte[] data = encoding.GetBytes(postData);
                // Prepare web request
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://203.81.21.34//send/gsend.asp?name=" + accountname + "&pwd=" + accountpwd + "&dst=" + PhoneNumber + "&msg=" + SMSContent);
                myRequest.Method = "POST";
                myRequest.Timeout = 10000;
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data.
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                //接收返回信息：
                HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
                StreamReader sreader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                tmp = sreader.ReadToEnd();
            }
            return tmp;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}